﻿$(function () {

    var mainScreen = {

        init: function () {
            this.discoverChildren();
            this.bindEvents();
            this.storeSelf();
        },

        discoverChildren: function () {
            this.$element = $("#screen");
            this.$pages = this.$element.find(".screen-page");
        },

        bindEvents: function () {
            EVENTBROKER.menuTopic.on(TOPIC_EVENTS.screenGo, this.onGoMenu.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchStarted, this.onMatchStarted.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchEnded, this.onMatchEnded.bind(this));
        },

        storeSelf: function () {
            this.$element.data("instance", this);
        },

        showPage: function (pageId) {
            this.$pages.removeClass("visible");
            this.$element.find("#" + pageId).addClass("visible");
        },

        onGoMenu: function (ev, menuId) {
            this.showPage(menuId);
        },

        onMatchStarted: function (ev, matchModel) {
            this.showPage("grid");
        },

        onMatchEnded: function (ev, matchModel) {
            this.showPage("home-screen");
        }
    }

    mainScreen.init();

});
